import { defineStore } from 'pinia'
import { RouteRecordRaw } from 'vue-router'
import { MenuInfo, MenuList } from '#/comm/menu'
import { getUserMenuList } from '@/service/user'
import { getMenuResultInfo } from '@/mock/menu'

const useMenuStore = defineStore('menu', {
  state: () => ({
    // 系统返回菜单和权限
    menuInfo: null as MenuInfo | null,
    // 项目菜单
    routerMenuList: null as RouteRecordRaw[] | null,
  }),
  getters: {
    // 所有权限
    getMenuInfo(state) {
      return state.menuInfo
    },
    // 菜单权限列表
    getMenuList(state): MenuList[] {
      return state.menuInfo?.menuList || []
    },
    // 按钮权限列表
    getResourceGroupList(state) {
      return state.menuInfo?.resourceGroupList || []
    },
    // 获取项目路由
    getRouterMenuList(state) {
      return state.routerMenuList
    },
  },
  actions: {
    // 初始化菜单
    async getUserMenuList() {
      if (!this.menuInfo) {
        const result = getMenuResultInfo()
        // const result = await getUserMenuList()
        if (result) {
          this.menuInfo = result as unknown as MenuInfo
        }
      }
    },
    // setUserMenuList(menuInfo: MenuInfo) {
    //   this.menuInfo = menuInfo
    // },
    // 设置项目路由
    setRouterMenusList(menuList: RouteRecordRaw[]) {
      this.routerMenuList = menuList
    },
  },
})

export default useMenuStore
